package com.wl.dzt.sys.core.dao.tree;

import com.wl.dzt.sys.util.SpringUtil;

import java.util.Arrays;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 树形设置器
 * 配置树形数据结构的基本设置参数
 */
public class TreeSetting {

    /**
     * 编号属性
     */
    private String idField;
    /**
     * 文本属性
     */
    private String textField;
    /**
     * 上级属性
     */
    private String parentIdField;
    /**
     * 扩展属性
     */
    private String[] expanFields;
    /**
     * 树形设置器
     */
    private TreeMapper mapper;
    /**
     * 开始遍历节点id
     */
    private String startId = "root";
    /**
     * 是否异步
     */
    private boolean async = false;
    //
    /**
     * 映射表达式
     */
    private String exp;
    /**
     * 包含自己
     */
    private boolean containOneself = false;

    private String[] fields;

    /**
     * <b>根据表达式创建树形设置器</b>
     * <p>
     * 表达式规则:id,name,pid,age,gender...<br/>
     * 第一位表示id属性字段<br/>
     * 第二位表示text属性字段<br/>
     * 第三位表示父节点编号字段,若为*则表示无视层级关系<br/>
     * 第四位开始包含第四位表示扩展字段可无限扩展下去<br/>
     * </p>
     *
     * @param exp 表达式
     * @return 树形设置器
     */
    public static TreeSetting createByExp(String exp) {
        TreeSetting setting = new TreeSetting();
        // 若是#开头则为springbean
        if ("#".equalsIgnoreCase(exp.substring(0, 1))) {
            TreeMapper mapper = SpringUtil.getBean(exp.replace("#", ""));
            setting.mapper = mapper;
            return setting;
        }

        String ary[] = exp.split(",");
        setting.idField = ary[0];
        setting.textField = ary[1];
        setting.parentIdField = ary[2];
        setting.mapper = TreeBuilder.createRelationMapper(setting);
        if (ary.length > 3) {
            setting.expanFields = Arrays.copyOfRange(ary, 3, ary.length);
        }
        return setting;
    }

    public String getIdField() {
        return idField;
    }

    public String getTextField() {
        return textField;
    }

    public String getParentIdField() {
        return parentIdField;
    }

    public String[] getExpanFields() {
        return expanFields;
    }

    public TreeMapper getMapper() {
        return mapper;
    }

    public void setMapper(TreeMapper mapper) {
        this.mapper = mapper;
    }

    public String getStartId() {
        return startId;
    }

    public void setStartId(String startId) {
        this.startId = startId;
    }

    public boolean isAsync() {
        return async;
    }

    public void setAsync(boolean async) {
        this.async = async;
    }

    public String getExp() {
        return exp;
    }

    public void setIdField(String idField) {
        this.idField = idField;
    }

    public void setTextField(String textField) {
        this.textField = textField;
    }

    public void setParentIdField(String parentIdField) {
        this.parentIdField = parentIdField;
    }

    public void setExpanFields(String[] expanFields) {
        this.expanFields = expanFields;
    }

    public void setExp(String exp) {
        this.exp = exp;
    }

    public boolean isContainOneself() {
        return containOneself;
    }

    public void setContainOneself(boolean containOneself) {
        this.containOneself = containOneself;
    }

    public String[] getFields() {
        return fields;
    }

    public void setFields(String[] fields) {
        this.fields = fields;
    }
}
